package com.qk.management.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.common.constant.BusinessStatusConstants;
import com.qk.domain.business.BusinessDO;
import com.qk.domain.business.BusinessPoolDO;
import com.qk.dto.business.BusinessAddDTO;
import com.qk.dto.business.BusinessDTO;
import com.qk.dto.business.BusinessFollowDTO;
import com.qk.dto.business.BusinessPoolDTO;
import com.qk.entity.Business;
import com.qk.management.aop.anno.LogAnno;
import com.qk.management.service.BusinessService;
import com.qk.vo.business.BusinessVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * @Author ZgYn
 * @Date 2025/10/15 20:22
 * @Name BusinessController
 */
@RestController
@RequestMapping("/businesses")
public class BusinessController {
    @Autowired
    private BusinessService businessService;

    @GetMapping
    public Result businessList(BusinessDTO dto) {
        PageResult<BusinessDO> list = businessService.getList(dto);
        return Result.success(list);
    }

    @LogAnno
    @PostMapping
    public Result addBusiness(@RequestBody BusinessAddDTO dto) {
        businessService.addBusiness(dto);
        return Result.success();
    }

    @LogAnno
    @PutMapping("/assign/{businessId}/{userId}")
    public Result assignBusiness(@PathVariable Integer businessId, @PathVariable Integer userId) {
        businessService.updateById(Business.builder()
                .id(businessId)
                .userId(userId)
                .status(BusinessStatusConstants.FOLLOWING)
                .updateTime(LocalDateTime.now())
                .build());
        return Result.success();
    }

    @LogAnno
    @PutMapping("/back/{id}")
    public Result backBusiness(@PathVariable Integer id) {
        businessService.backToPool(id);

        return Result.success();
    }

    @LogAnno
    @PostMapping("/toCustomer/{id}")
    public Result toCustomer(@PathVariable Integer id) {
        businessService.toCustomer(id);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getBusiness(@PathVariable Integer id) {
        BusinessVO vo = businessService.getBusinessById(id);
        return Result.success(vo);
    }

    @LogAnno
    @PutMapping
    public Result businessFollow(@RequestBody BusinessFollowDTO dto) {
        businessService.businessFollow(dto);
        return Result.success();
    }

    @GetMapping("/pool")
    public Result getPool(BusinessPoolDTO dto) {
        PageResult<BusinessPoolDO> list = businessService.getPool(dto);
        return Result.success(list);
    }
}
